package org.jeecg.modules.inventory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.BeanPlusUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.estar.tw.util.StringUtils;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.service.IBussInventoryDetailsService;
import org.jeecg.modules.inventory.service.IBussInventoryInoutDetailsService;
import org.jeecg.modules.inventory.vo.*;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 库存明细
 * @Author: jeecg-boot
 * @Date:   2024-07-03
 * @Version: V1.0
 */
@Api(tags="库存明细")
@RestController
@RequestMapping("/detail/bussInventoryDetails")
@Slf4j
public class BussInventoryDetailsController {
	@Autowired
	private IBussInventoryDetailsService bussInventoryDetailsService;
	@Autowired
	private IBussInventoryInoutDetailsService bussInventoryInoutDetailsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param bussInventoryDetails
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库存明细-分页列表查询")
	@ApiOperation(value="库存明细-分页列表查询", notes="库存明细-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BussInventoryDetails>> queryPageList(BussInventoryDetails bussInventoryDetails,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BussInventoryDetails> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetails, req.getParameterMap());
		Page<BussInventoryDetails> page = new Page<BussInventoryDetails>(pageNo, pageSize);
		IPage<BussInventoryDetails> pageList = bussInventoryDetailsService.myPage(page, queryWrapper);
		return Result.OK(pageList);
	}

	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "库存明细-通过id查询")
	@ApiOperation(value="库存明细-通过id查询", notes="库存明细-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BussInventoryDetails> queryById(@RequestParam(name="id",required=true) String id) {
		BussInventoryDetails bussInventoryDetails = bussInventoryDetailsService.getById(id);
		if(bussInventoryDetails==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bussInventoryDetails);

	}

	 @ApiOperation(value="库存明细-分页列表查询", notes="库存明细-分页列表查询")
	 @GetMapping(value = "/warehouseList")
	 public Result<IPage<BussInventoryDetailsVo>> queryWarehouseList(BussInventoryDetailsVo bussInventoryDetails,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																	 HttpServletRequest req) {
		 IPage<BussInventoryDetailsVo> pageList = new Page<>();
		 //如果没选仓库直接返回空
		 if(StringUtils.isEmpty(bussInventoryDetails.getWarehouseCode())){
			 return Result.OK(pageList);
		 }
		 QueryWrapper<BussInventoryDetailsVo> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetails, req.getParameterMap());
		 Page<BussInventoryDetailsVo> page = new Page<BussInventoryDetailsVo>(pageNo, pageSize);
		 pageList = bussInventoryDetailsService.queryWarehouseList(page, queryWrapper);
		 return Result.OK(pageList);

	 }

	@RequestMapping(value = "/exportWarehouseXls")
	public ModelAndView exportWarehouseXls(HttpServletRequest req, BussInventoryDetailsVo bussInventoryDetails,
								  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		// Step.1 组装查询条件查询数据
		IPage<BussInventoryDetailsVo> pageList = new Page<>();
		QueryWrapper<BussInventoryDetailsVo> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetails, req.getParameterMap());
		Page<BussInventoryDetailsVo> page = new Page<BussInventoryDetailsVo>(pageNo, pageSize);
		pageList = bussInventoryDetailsService.queryWarehouseList(page, queryWrapper);
		//Step.2 获取导出数据
		List<BussInventoryDetailsVo> records = pageList.getRecords();

		List<BussInventoryDetailsWarehouseVo> bussInventoryDetailsWarehouseVos = BeanPlusUtil.toBeanList(records, BussInventoryDetailsWarehouseVo.class);

		// Step.4 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "物料库位管理列表");
		mv.addObject(NormalExcelConstants.CLASS, BussInventoryDetailsWarehouseVo.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("物料库位管理数据", "导出人:"+sysUser.getRealname(), "物料库位管理"));
		mv.addObject(NormalExcelConstants.DATA_LIST, bussInventoryDetailsWarehouseVos);
		return mv;
	}

	 @AutoLog(value = "库位管理")
	 @ApiOperation(value="库位管理-", notes="库位管理")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody BussInventoryDetailsPage bussInventoryDetailsPage){
		 bussInventoryDetailsService.saveMain(bussInventoryDetailsPage);
		 return Result.OK("编辑成功！");
	 }


	 @ApiOperation(value="获取库存-", notes="获取库存")
	 @GetMapping(value = "/getkc")
	 public Result<IPage<BussInventoryDetailsVo>> getKC(BussInventoryDetails bussInventoryDetails,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req){
		 QueryWrapper<BussInventoryDetails> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetails, req.getParameterMap());
		 Page<BussInventoryDetails> page = new Page<BussInventoryDetails>(pageNo, pageSize);
		 IPage<BussInventoryDetailsVo> pageList = bussInventoryDetailsService.selectKC(page, queryWrapper);
		 return Result.OK(pageList);
	 }


	 /**
	  * 扣货管理列表查询
	  *
	  * @param bussInventoryDetails
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "库存明细-分页列表查询")
	 @ApiOperation(value="库存明细-分页列表查询", notes="库存明细-分页列表查询")
	 @GetMapping(value = "/deduct/list")
	 public Result<IPage<BussInventoryDetailsPageVo>> deductPageList(BussInventoryDetailsPageVo bussInventoryDetails,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																	 HttpServletRequest req) {
		 QueryWrapper<BussInventoryDetailsPageVo> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetails, req.getParameterMap());
		 Page<BussInventoryDetailsPageVo> page = new Page<BussInventoryDetailsPageVo>(pageNo, pageSize);
		 IPage<BussInventoryDetailsPageVo> pageList = bussInventoryDetailsService.deductPage(page, queryWrapper);
		 return Result.OK(pageList);
	 }


	/**
	 * 导出excel
	 *
	 * @param request
	 * @param bussInventoryDetailsPageVo
	 */
	//@RequiresPermissions("org.jeecg.modules.demo:buss_storage_notice:exportXls")
	@RequestMapping(value = "/deduct/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, BussInventoryDetailsPageVo bussInventoryDetailsPageVo,
								  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		// Step.1 组装查询条件查询数据
		QueryWrapper<BussInventoryDetailsPageVo> queryWrapper = QueryGenerator.initQueryWrapper(bussInventoryDetailsPageVo, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<BussInventoryDetailsPageVo> page = new Page<BussInventoryDetailsPageVo>(pageNo, pageSize);
		//配置选中数据查询条件
		String selections = request.getParameter("selections");
		if(oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("id",selectionList);
		}
		//Step.2 获取导出数据
		IPage<BussInventoryDetailsPageVo> bussInventoryDetailsPageVoIPage = bussInventoryDetailsService.deductPage(page, queryWrapper);
		List<BussInventoryDetailsPageVo> records = bussInventoryDetailsPageVoIPage.getRecords();

		List<BussInventoryDetailsExportPageVo> bussInventoryDetailsExportPageVos = BeanPlusUtil.toBeanList(records, BussInventoryDetailsExportPageVo.class);

		// Step.4 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "扣货管理列表");
		mv.addObject(NormalExcelConstants.CLASS, BussInventoryDetailsExportPageVo.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("扣货管理数据", "导出人:"+sysUser.getRealname(), "扣货管理单"));
		mv.addObject(NormalExcelConstants.DATA_LIST, bussInventoryDetailsExportPageVos);
		return mv;
	}



 }
